<?php

namespace app\index\controller;

use think\Db;
use think\upload;
use \think\File;

class Apply extends Base
{
//单据申请首页
    public function index()
    {
        $this->baseindex();
        /*$ch_name = $_SESSION['ch_name'];
        $id = $_SESSION['id'];
        $res = Db::table('oa_user')->field('region_id,branch_id')->where(['ch_name' => $ch_name, 'id' => $id])
            ->find();
        $bill_type_id = 1;
        $where = "bill_type_id=" . $bill_type_id . " and ( ( region_id=" . $res['region_id']
            . " and ( branch_id=" . $res['branch_id'] . " or branch_id =18 ) )
            or ( region_id = 2 ) and ( branch_id = 18 or branch_id= " . $res['branch_id'] . ") )";
        $flow = Db::table('oa_flow')->where($where)->select();
        foreach ($flow as $key => $value) {
            if (!empty($value['first'])) {
                $first = Db::table('oa_role')->where('id=' . $value['first'])->find();
                $flow[$key]['first'] = $first['role'];
                $flow[$key]['flow'] = $first['role'];
            } else {
                unset($flow[$key]['first']);
            }
            if (!empty($value['second'])) {
                $second = Db::table('oa_role')->where('id=' . $value['second'])->find();
                $flow[$key]['second'] = $second['role'];
                $flow[$key]['flow'] .= '->' . $second['role'];
            } else {
                unset($flow[$key]['second']);
            }
            if (!empty($value['third'])) {
                $third = Db::table('oa_role')->where('id=' . $value['third'])->find();
                $flow[$key]['third'] = $third['role'];
                $flow[$key]['flow'] .= '->' . $third['role'];
            } else {
                unset($flow[$key]['third']);
            }
            if (!empty($value['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $value['fourth'])->find();
                $flow[$key]['fourth'] = $fourth['role'];
                $flow[$key]['flow'] .= '->' . $fourth['role'];
            } else {
                unset($flow[$key]['fourth']);
            }
            if (!empty($value['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $value['fifth'])->find();
                $flow[$key]['fifth'] = $fifth['role'];
                $flow[$key]['flow'] .= '->' . $fifth['role'];
            } else {
                unset($flow[$key]['fifth']);
            }
            if (!empty($value['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $value['sixth'])->find();
                $flow[$key]['sixth'] = $sixth['role'];
                $flow[$key]['flow'] .= '->' . $sixth['role'];
            } else {
                unset($flow[$key]['sixth']);
            }
            if (!empty($value['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $value['seventh'])->find();
                $flow[$key]['seventh'] = $seventh['role'];
                $flow[$key]['flow'] .= '->' . $seventh['role'];
            } else {
                unset($flow[$key]['seventh']);
            }
            if (!empty($value['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $value['eighth'])->find();
                $flow[$key]['eighth'] = $eighth['role'];
                $flow[$key]['flow'] .= '->' . $eighth['role'];
            } else {
                unset($flow[$key]['eighth']);
            }
            if (!empty($value['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $value['ninth'])->find();
                $flow[$key]['ninth'] = $ninth['role'];
                $flow[$key]['flow'] .= '->' . $ninth['role'];
            } else {
                unset($flow[$key]['ninth']);
            }
            if (!empty($value['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $value['tenth'])->find();
                $flow[$key]['tenth'] = $tenth['role'];
                $flow[$key]['flow'] .= '->' . $tenth['role'];
            } else {
                unset($flow[$key]['tenth']);
            }
        }*/
        $user_id = $_SESSION['id'];
        //审批人信息
        $res = Db::table('oa_user u,oa_branch b')->field('u.ch_name,u.position,b.branch,u.region_id')->where('u.branch_id=b.id and u.id=' . $user_id)
            ->find();
        //费用报销流程
        $flow_reimbursement_apply = $this->flow_reimbursement_apply();
        //支付审批流程
        $flow_payment_application_form = $this->flow_payment_application_form();
        //请假流程
        $flow_leave = $this->flow_leaves();
        //合同流程
        $flow_contract_form = $this->flow_contract_form();
        //请假类型
        $leave = Db::table('oa_type_leave')->where('state=1')->select();
        //合同类型
        $contract_type = Db::table('oa_contract_type')->where('state=1')->select();
        //提交区域
        $contract_region = Db::table('oa_region')->where('state=1')->select();
        $this->assign('myself_list', $res);
        $this->assign('flow_reimbursement_apply', $flow_reimbursement_apply);
        $this->assign('flow_payment_application_form', $flow_payment_application_form);
        $this->assign('flow_leave', $flow_leave);
        $this->assign('flow_contract_form', $flow_contract_form);
        $this->assign('leave', $leave);
        $this->assign('contract_type', $contract_type);
        $this->assign('contract_region', $contract_region);
        return $this->fetch();
    }

    //上传
    public function upload()
    {
        //上传文件
        session_start();
        $username = $_SESSION['en_name'];
        $file_name_a = $_FILES['file']['name'];
        //$rand = time();
        $rand = date('His');
        //上传文件目录
        $file_name_b = date('Y-m-d');
        //上传文件名字
        $file_name_c = $rand . '-' . $file_name_a;
        //return json_encode($file_name['name']);
        //获取表单上传文件
        $file = request()->file('file');
        //return $file;
        if (empty($file)) {
            $arr = ['code' => 1, 'msg' => "格式错误", 'data' => ['src' => "{:url('Apply/upload')}"]];
            return $arr;
        }
        $file_name = iconv('utf-8', 'gb2312', $file_name_c); //利用Iconv函数对文件名进行重新编码
        $name = iconv('utf-8', 'gb2312', ROOT_PATH . 'public/upload/' . $username . '/' . $file_name_b); //利用Iconv函数对文件名进行重新编码

        //移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move($name, $file_name);
        //$name=iconv("gb2312","UTF-8", $info);
        if ($info) {
            $_SESSION['wenjia_name'] = $file_name_c;
            $_SESSION['wenjia_mulu'] = $file_name_b;
            $session = $_SESSION['wenjia_name'];
            $arr = ['code' => 0, 'msg' => "$session", 'data' => ['src' => "{:url('Apply/upload')}"]];
            return $arr;
        } else {
            //上传失败获取错误信息
            $arr = ['code' => 1, 'msg' => "12", 'data' => ['src' => "{:url('Apply/upload')}"]];
            return $arr;
        }
    }

    //多文件上传
    public function uploads()
    {
        //上传文件
        session_start();
        if (!empty($_SESSION['contract_form_id'])){
            $username = $_SESSION['en_name'];
            $contract_fj_id = $_SESSION['contract_form_id'];
            $file_name_a = $_FILES['file']['name'];
            $rand = date('His');
            //上传文件目录
            $file_name_b = date('Y-m-d');
            //上传文件名字
            $file_name_c = $rand . '-' . $file_name_a;
            //获取表单上传文件
            $file = request()->file('file');
            if (empty($file)) {
                $arr = ['code' => 0, 'msg' => "格式错误", 'data' => ['src' => "{:url('Apply/upload')}"]];
                return $arr;
            }
            $file_name = iconv('utf-8', 'gb2312', $file_name_c); //利用Iconv函数对文件名进行重新编码
            $name = iconv('utf-8', 'gb2312', ROOT_PATH . 'public/upload/' . $username . '/' . $file_name_b); //利用Iconv函数对文件名进行重新编码

            //移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move($name, $file_name);
            //$name=iconv("gb2312","UTF-8", $info);
            if ($info) {
                $res=Db::table('oa_contract_enclosure')->insert(['contract_fj_name' => $file_name_c, 'contract_form_id' => $contract_fj_id,'contract_fj_lujing'=>$file_name_b]);
                if ($res){
                    $arr = ['code' => 0, 'msg' => "上传成功", 'data' => ['src' => "{:url('Apply/uploads')}"]];
                    //unset($_SESSION['contract_form_id']);
                    return $arr;
                }else{
                    $arr = ['code' => 0, 'msg' => "上传数据失败", 'data' => ['src' => "{:url('Apply/uploads')}"]];
                    return $arr;
                }
            } else {
                //上传失败获取错误信息
                $arr = ['code' => 0, 'msg' => "上传失败", 'data' => ['src' => "{:url('Apply/uploads')}"]];
                return $arr;
            }
        }else{
            $arr = ['code' => 0, 'msg' => "请先提交表单", 'data' => ['src' => "{:url('Apply/uploads')}"]];
            return $arr;
        }

    }

    //单据申请
    public function apply()
    {
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            $post = input('post.data');
            $res = json_decode($post, true);
            //return $res;
            //判断单据类型掉下面方法处理提交的申请
            if ($res['bill_type_id'] == 1) {
                //支付报销申请
                $result = $this->reimbursement_apply($res);
                return $result;
            } elseif ($res['bill_type_id'] == 2) {
                //付款申请
                $result = $this->payment_application_form($res);
                return $result;
            } elseif ($res['bill_type_id'] == 4) {
                //请假申请
                $result = $this->leaves_form($res);
                return $result;
            } elseif ($res['bill_type_id'] == 8) {
                //合同添加
                $result = $this->contract_form($res);
                return $result;
            }
        }
    }

    //处理提交付款申请表的申请
    public function reimbursement_apply($res)
    {
        session_start();
        $nums = 0;
        $purposes = '';
        //$post = input('post.data');
        if (isset($_SESSION['wenjia_name'])) {
            $session = $_SESSION['wenjia_name'];
            $sessions = $_SESSION['wenjia_mulu'];
            //上传文件名字
            $res['bill'] = $session;
            //上传文件目录
            $res['bills'] = $sessions;
        }
        //return $res['money_a'];

        $res['user_id'] = $_SESSION['id'];
        //申请费用要大于0
        if ($res['total'] > 0) {
            //取出审批流程第一级审批的角色
            $first = Db::table('oa_flow')->field('first')->where(['id' => $res['flow_id']])->find();
            //flow键role是将要代表审批的级别，键role_key是审批的角色
            $flow_a['role'] = $first['first'];
            $flow_a['role_key'] = 'first';
            $flow_a['user_id'] = '';
            //audit_opinion审核人的审核意见;
            $flow_a['audit_opinion'] = '';
            //审核时间
            $flow_a['audit_time'] = '';
            //审核人的名字
            $flow_a['audit_name'] = '';
            $flow_a['state'] = 0;
            $flow[] = $flow_a;
            $res['apply_state'] = json_encode($flow);
            //添加报销信息
            $num = Db::table('oa_expense_reimbursement')->insertGetId($res);

            //处理报销具体的信息
            if ($num) {
                if (!empty($res['purpose_a']) && !empty($res['money_a'])) {
                    $purpose['purpose'] = $res['purpose_a'];
                    $purpose['money'] = $res['money_a'];
                    $purpose['expense_reimbursement_id'] = $num;
                    $purposes[] = $purpose;
                }
                if (!empty($res['purpose_b']) && !empty($res['money_b'])) {
                    $purpose['purpose'] = $res['purpose_b'];
                    $purpose['money'] = $res['money_b'];
                    $purpose['expense_reimbursement_id'] = $num;
                    $purposes[] = $purpose;
                }
                if (!empty($res['purpose_c']) && !empty($res['money_c'])) {
                    $purpose['purpose'] = $res['purpose_c'];
                    $purpose['money'] = $res['money_c'];
                    $purpose['expense_reimbursement_id'] = $num;
                    $purposes[] = $purpose;
                }
                if (!empty($res['purpose_d']) && !empty($res['money_d'])) {
                    $purpose['purpose'] = $res['purpose_d'];
                    $purpose['money'] = $res['money_d'];
                    $purpose['expense_reimbursement_id'] = $num;
                    $purposes[] = $purpose;
                }
                if (!empty($res['purpose_e']) && !empty($res['money_e'])) {
                    $purpose['purpose'] = $res['purpose_e'];
                    $purpose['money'] = $res['money_e'];
                    $purpose['expense_reimbursement_id'] = $num;
                    $purposes[] = $purpose;
                }
                unset($_SESSION['wenjia_name']);
                //添加报销详情
                foreach ($purposes as $k => $v) {
                    $num_a = Db::table('oa_expense_reimbursement_cost')->insert($purposes[$k]);
                    $nums = $num_a + 1;
                }
                return $nums;
            } else {
                return $nums;
            }
        } else {
            return $nums;
        }
    }

    //处理提交费用报销单的申请
    public function payment_application_form($res)
    {
        //return 1;
        session_start();
        $first = Db::table('oa_flow')->field('first')->where(['id' => $res['payment_application_form_flow_id']])->find();
        //flow键role是将要代表审批的级别，键role_key是审批的角色
        $flow_a['role'] = $first['first'];
        $flow_a['role_key'] = 'first';
        $flow_a['user_id'] = '';
        //audit_opinion审核人的审核意见;
        $flow_a['audit_opinion'] = '';
        //审核时间
        $flow_a['audit_time'] = '';
        //审核人的名字
        $flow_a['audit_name'] = '';
        $flow_a['state'] = 0;
        $flow[] = $flow_a;
        $res['user_id'] = $_SESSION['id'];
        $res['apply_state'] = json_encode($flow);
        $result = Db::table('oa_payment_application_form')->insert($res);
        return $result;
    }

    //处理提交请假申请表的申请
    public function leaves_form($res)
    {
        session_start();
        $first = Db::table('oa_flow')->field('first')->where(['id' => $res['leave_flow_id']])->find();
        //flow键role是将要代表审批的级别，键role_key是审批的角色
        $flow_a['role'] = $first['first'];
        $flow_a['role_key'] = 'first';
        $flow_a['user_id'] = '';
        //audit_opinion审核人的审核意见;
        $flow_a['audit_opinion'] = '';
        //审核时间
        $flow_a['audit_time'] = '';
        //审核人的名字
        $flow_a['audit_name'] = '';
        $flow_a['state'] = 0;
        $flow[] = $flow_a;
        $res['user_id'] = $_SESSION['id'];
        $res['apply_state'] = json_encode($flow);
        $result = Db::table('oa_leaves_form')->insert($res);
        return $result;
    }

    //处理提交请假申请表的申请
    public function contract_form($res)
    {
        session_start();
        $first = Db::table('oa_flow')->field('first')->where(['id' => $res['contract_flow_id']])->find();
        //flow键role是将要代表审批的级别，键role_key是审批的角色
        $flow_a['role'] = $first['first'];
        $flow_a['role_key'] = 'first';
        $flow_a['user_id'] = '';
        //audit_opinion审核人的审核意见;
        $flow_a['audit_opinion'] = '';
        //审核时间
        $flow_a['audit_time'] = '';
        //审核人的名字
        $flow_a['audit_name'] = '';
        $flow_a['state'] = 0;
        $flow[] = $flow_a;
        $res['user_id'] = $_SESSION['id'];
        $res['apply_state'] = json_encode($flow);
        $result = Db::table('oa_contract_form')->insertGetId($res);
        $_SESSION['contract_form_id'] = $result;
        return $result;
    }
}